﻿using CefSharp;
using CefSharp.WinForms;

namespace MrytPub
{
    internal class OpenPageSelf : ILifeSpanHandler
    {
        public bool DoClose(IWebBrowser browserControl, IBrowser browser)
        {
            return false;
        }

        public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
        {

        }

        public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
        {

        }

        public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
        {
            newBrowser = null;
            var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
            if (targetUrl.StartsWith("https://zujuan.21cnjy.com/question/detail/"))
            {
                PubForm pubForm = new PubForm(targetUrl, MainForm.curDay);
                pubForm.Show();
            }
            else
            {
                chromiumWebBrowser.Load(targetUrl);
            }
            return true; //Return true to cancel the popup creation copyright by codebye.com.
        }
    }
}